package com.zr.d_c.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.d_c.service.CustomerService;
import com.zr.d_c.service.SellerService;
import com.zr.d_c.service.UserService;
import com.zr.d_c.serviceimpl.CustomerServiceImpl;
import com.zr.d_c.serviceimpl.SellerServiceImpl;
import com.zr.d_c.serviceimpl.UserServiceImpl;

public class ResetUpswController extends HttpServlet {

	CustomerService customerservice = new CustomerServiceImpl();
	SellerService sellerservice = new SellerServiceImpl();
	UserService userservice = new UserServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter pw = resp.getWriter();
		String sidstr = req.getParameter("sids");
		String cidstr = req.getParameter("cids");
		if(cidstr!=null){
			String[] cidst = cidstr.split(",");
			int[] cids = new int[cidst.length];
			for (int i = 0; i < cidst.length; i++) {
				cids[i] = Integer.parseInt(cidst[i]);
			}
			for (int i = 0; i < cids.length; i++) {
				userservice.resetUpswByUid(customerservice.getCustomerByCid(cids[i]).getUid());
			}
			pw.write("1");
		}else if(sidstr!=null){
			String[] sidst = sidstr.split(",");
			int[] sids = new int[sidst.length];
			for (int i = 0; i < sidst.length; i++) {
				sids[i] = Integer.parseInt(sidst[i]);
			}
			for (int i = 0; i < sids.length; i++) {
				userservice.resetUpswByUid(sellerservice.getSellerBySid(sids[i]).getUid());
			}
			pw.write("1");
		}else{
			pw.write("0");
		}
		pw.close();
	}
}
